﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ServerTesting
{
    class Program
    {
        static void Main(string[] args)
        {

            _calledTimes = 0;


            for (int i = 0; i < 10000; i++)
            {
                var task = new Task(CallService);
                task.Start();
            }

            Console.ReadKey();
        }

        private static int _calledTimes;

        private static List<string>  Sites = new List<string>()
            {
                "http://192.168.81.63/iTeamPublish/",
                "https://projectconnect.ncs.com.sg/site/index.do",
                "https://timelog.ncs.com.sg/ProjectSetup/ProjectListing.aspx"
            };


        private static void Test()
        {
            _calledTimes++;
            Console.WriteLine(_calledTimes);
        }

        private static void CallService()
        {
            using (var client = new HttpClient())
            {

                var buffer = Encoding.ASCII.GetBytes(@"ncs\xinzhe:Pass2026");
                var authHeader = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(buffer));
                client.DefaultRequestHeaders.Authorization = authHeader;

                var number = new Random(3).Next(0, 2);
                var task = client.GetAsync(Sites[number]);


                if (task.Result.StatusCode == HttpStatusCode.Unauthorized)
                {
                    Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
                }
                else
                {
                    task.Result.EnsureSuccessStatusCode();
                    _calledTimes++;
                    Console.WriteLine(_calledTimes);
                }
            }
        }
    }
}
